﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using IntelligentPharmacy.publicclass;
using SmartForcer;
using SmartRoom;
using System.Threading;
using System.Runtime.InteropServices;

namespace ConsumablesCabinet
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        System.Windows.Forms.Timer timer1 = null;
        int AutoExitTimer = 300;//默认5分钟
        protected override void OnStartup(StartupEventArgs e)
        {
            
            /* Global.LoadConfig();
             MenuWindows menuWindows = new MenuWindows();
             menuWindows.ShowDialog();*/

            try
            {
                Engine.InitEngines();//初始化人脸识别引擎
                if (timer1 == null)
                {
                    timer1 = new System.Windows.Forms.Timer();
                }
                timer1.Interval = 5000;
                timer1.Tick += new EventHandler(timer1_Tick);
                timer1.Start();
            }
            catch
            {
                MessageBox.Show("初始化人脸识别错误");
            }
            string appName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
            int processCount = Process.GetProcessesByName(appName).Where(o => o.Id != Process.GetCurrentProcess().Id).Count();
            if (processCount > 0)
            {
                MessageBox.Show("当前程序已经在运行，不能重复启动！");
                Application.Current.Shutdown();
            }
            else
            {
                // string macAddress = DeviceInfo.GetMacAddress();//GetCpuID();
                if (string.IsNullOrEmpty(ReadAppConfig.readerWarehouseName()))
                {
                    CheckLisenseWindow checkLisenseWindow = new CheckLisenseWindow();
                    checkLisenseWindow.ShowDialog();
                }
                else
                {
                    MainWindow mainWindow1 = new MainWindow();
                    mainWindow1.ShowDialog();
                }
                Application.Current.Shutdown();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                //判断空闲时间是否超过15分钟,超过则自动弹出视频播放窗口
                if (GetIdleTick() / 1000 >= AutoExitTimer)
                {
                    if (Global.WindowsList.Count > 0)
                    {
                      
                    }

                    // foreach (Window obj in Global.WindowsList)
                    //   obj.Close();
                }
            }
            catch (Exception ex)
            { }
        }

        /// <summary>
        /// 获取鼠标键盘空闲时间
        /// </summary>
        /// <returns></returns>
        public long GetIdleTick()
        {
            if (Global.SystemIdelTime == 0)
            {
                Global.SystemIdelTime = 1;
                return 0;
            }
            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
            if (!GetLastInputInfo(ref lastInputInfo)) return 0;
            return Global.SystemIdelTime = Environment.TickCount - (long)lastInputInfo.dwTime;
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct LASTINPUTINFO
        {
            [MarshalAs(UnmanagedType.U4)]
            public int cbSize;
            [MarshalAs(UnmanagedType.U4)]
            public uint dwTime;
        }
        /// <summary>
        /// 调用windows API获取鼠标键盘空闲时间
        /// </summary>
        /// <param name="plii"></param>
        /// <returns></returns>
        [DllImport("user32.dll")]
        private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
    }
}
